<?php

namespace app\common\model\task;

use app\common\model\User;
use think\Exception;
use think\Model;


class Lists extends Model
{

    // 表名
    protected $table = 'task_lists';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'type_text'
    ];


    public static function init()
    {
        self::beforeInsert(function ($row) {
            if ($row['type'] == 1) {
                throw new Exception("引导任务不可新增");
            }
        });

        self::beforeUpdate(function ($row) {
            $origin = $row->getOriginData();
            $changedata = $row->getChangedData();
            if (isset($changedata['type'])) {
                if ($origin['type'] == 1) {
                    throw new Exception("引导任务类型不可变更");
                }

                if ($changedata['type'] == 1) {
                    throw new Exception("引导任务类型不可选");
                }
            }
            if (isset($changedata['award_type']) && $changedata['award_type'] != 1 && $origin['type'] == 1) {
                throw new Exception("引导任务内奖励类型仅限银币");
            }

            if (isset($changedata['date_limit'])) {
                if ($origin['type'] == 1 && $changedata['date_limit'] != 'more') {
                    throw new Exception("引导任务时间限制不可更改");
                }
            }

        });

        self::beforeDelete(function ($row) {
            $type = self::where('id', $row['id'])->value('type');
            if ($type == 1) {
                throw new Exception("引导任务不可删除");
            }
        });
    }

    public function getOriginData()
    {
        return $this->origin;
    }

    //用户任务列表
    public static function user_list($user_id)
    {
        $type = self::type();
        $result = [];
        $guide = 0;
        foreach ($type as $key => $value) {
            $switch = [];
            if ($key != 1) {
                $switch['switch'] = 1;
            }
            $task = self::where(['type' => $key])->field("createtime,updatetime", true)->where($switch)->select();
            foreach ($task as $tk => $item) {
                $task[$tk]['status'] = Userlog::check($item, $user_id);
                if ($item['type'] == 1 && $task[$tk]['status'] != 1) {
                    $guide = -1;
                }
            }
            $result[$value] = $task;
        }

        $is_guide_receive = User::where('id', $user_id)->value('is_guide_receive');
        if ($guide == 0) {
            if ($is_guide_receive == 1) {
                $guide = 1;
            }
        }
        $result['guide_status'] = $guide;
        $result['guide_price'] = self::where('type', 1)->sum('amount');
        return $result;
    }

    public static function type()
    {
        return [1 => 'guide', 2 => 'system', 3 => 'custom'];
    }

    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getTypeList()
    {
        return ['1' => __('引导任务'), '2' => '系统任务', '3' => '自定义任务'];
    }

    public function getLocationList()
    {
        return ["invite" => "邀请注册", "topup" => "充值", "game" => "对局"];
    }

    public function getAwardTypeList()
    {
        return [1 => '银币', 2 => '金币', 3 => '积分', 4 => '钻石'];
    }

    public function getDateLimitList()
    {
        return ['today' => '日', 'week' => '周', 'month' => '月', 'more' => '不限制'];
    }
}
